import ajax from '../../libs/ajax.js';
import tools from '../../libs/tools.js';

//#region 处理好友信息
let ym = document.getElementById('ym');
let cs1 = {
  info: 'friend'
};
let page = {
  pageNumber: 1,
  pageSize: 5
};
let sz = [];
function clhyxx() {
  ajax.send('/user/auth/friendQuery', tools.concatJson(cs1, page), (data) => {
    if (data.success) {
      sz = data.list;
      page = data.page;
      zs();
    }
  });
}
let nr = document.getElementById('nr');
let btnadd=document.getElementById('btnadd')
let datalist = document.getElementById('datalist');
btnadd.addEventListener('click',()=>{
  nr.innerHTML=nr.value;
})
function zs() {
  console.log('dsfsfsfsfsfsfs');
  datalist.innerHTML = '';
  for (let i = 0; i < sz.length; i++) {
    const x = sz[i];
    console.log('内容', x);
    let div1 = document.createElement('div');
    div1.classList.add('item');
    let img1 = document.createElement('img');
    img1.classList.add('face');
    if (x.userInfo.img) {
      img1.setAttribute('src', x.userInfo.img);
    } else {
      img1.setAttribute(
        'src',
        'https://tse1-mm.cn.bing.net/th/id/OIP-C.N9b_ZTYGwIS-OBg-NHcmPAAAAA?pid=ImgDet&rs=1'
      );
    }
    div1.append(img1);
    datalist.append(div1);

    let div2 = document.createElement('div');
    div2.classList.add('des');
    let div3 = document.createElement('div');
    div3.classList.add('nickName');
    let span1 = document.createElement('span');
    span1.classList.add('denglu');
    span1.append(x.user.username);
    div3.append(span1);
    div2.append(div3);
    div1.append(div2);
    datalist.append(div1);

    //账号
    let span2 = document.createElement('span');
    span2.classList.add('denglu');
    span2.append(x.user.nickname);
    div3.append(span2);
    div2.append(div3);
    div1.append(div2);
    datalist.append(div1);

    //操作按钮
    //私信
    let span3 = document.createElement('span');
    span3.classList.add('btn');
    let button1 = document.createElement('button');
    button1.classList.add('btn', 'btn-danger', 'btn-sm', 'm-1');
    button1.setAttribute('data-bs-toggle', 'modal');
    button1.setAttribute('data-bs-target', '#sxDialog');
    button1.append('私聊');
    span3.append(button1);
    div3.append(span3);
    div2.append(div3);
    div1.append(div2);
    datalist.append(div1);
    button1.addEventListener('click', () => {
      let cs = {
        username: x.user.username,
        info: nr.value
      };
      ajax.send('/user/auth/sendMesage', cs, (data) => {
        if (data.success) {
          showToast(data.message);
          clhyxx();
        }
      });
    });

    //查询
    let span4 = document.createElement('span');
    span4.classList.add('btn');
    let button2 = document.createElement('button');
    button2.classList.add('btn', 'btn-danger', 'btn-sm', 'm-1');
    button2.setAttribute('data-bs-toggle', 'modal');
    button2.setAttribute('data-bs-target', '#ckdialog');
    button2.append('私聊信息查询');
    span3.append(button2);
    div3.append(span3);
    div2.append(div3);
    div1.append(div2);
    datalist.append(div1);
    button2.addEventListener('click', () => {
      hqsx(x);
    });

    let span5 = document.createElement('span');
    span5.classList.add('btn');
    let button3 = document.createElement('button');
    button3.classList.add('btn', 'btn-danger', 'btn-sm', 'm-1');
    button3.append('删除好友');
    span5.append(button3);
    div3.append(span5);
    div2.append(div3);
    div1.append(div2);
    datalist.append(div1);
    button3.addEventListener('click',()=>{
      let cs={
        username: x.user.username,
      }
      ajax.send('/user/auth/friendDelete',cs,(data)=>{
        if(data){
          showToast('删除成功')
          clhyxx();
        }
      })
    })
    //操作按钮
    // td = document.createElement('td');
    // let an = document.createElement('button');
    // an.append('同意');
    // an.addEventListener('click', () => {
    //   let cs = {
    //     info: '',
    //     username: x.user.username
    //   };
    //   ajax.send('/user/auth/friendAgree', cs, (data) => {
    //     if (data.message) {
    //       showToast(data.message);
    //     }
    //   });
    // });
    // td.append(an);
    // tr.append(td);

    // td = document.createElement('td');
    // let an1 = document.createElement('button');
    // an1.append('拒绝');
    // an1.addEventListener('click', () => {
    //   let cs = {
    //     info: 'deny',
    //     username: x.user.username
    //   };
    //   ajax.send('/user/auth/friendAgree', cs, (data) => {
    //     if (data.message) {
    //       showToast(data.message);
    //     }
    //   });
    // });
    // td.append(an1);
    // tr.append(td);
  }
}

//#endregion




//#region 获取私信

let css = [];
function hqsx(x) {
  let cs = {
    username: x.user.username
  };
  let page = {
    pageNumber: 1,
    pageSize: 10
  };
  
  ajax.send(
    '/user/auth/findPrivateMessageByUser',
    tools.concatJson(cs, page),
    (data) => {
      if (data.success) {
        page = data.page;
        css = data.list;
        sxzs();
        console.log('adccacac',css);
      }
      showToast(data.message);
    }
  );
}


function sxzs() {
  let tbdept=document.getElementById('tbdept');
  tbdept.innerHTML='';
  for (let i = 0; i < css.length; i++) {
    const e = css[i];
    let tr = document.createElement('tr');
    let td;
    //内容
    td = document.createElement('td');
    td.append(e.info);
    tr.append(td);

    //时间
    td = document.createElement('td');
    td.append(tools.FormData(e.lastupdate));
    tr.append(td);
    tbdept.append(tr);
  }
}
//#endregion

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion

clhyxx();
